﻿

db.MatrixCompany.insert([
{

    MatrixID: NUUID("00000000-0000-0000-0000-000000000000"),
    CompanyID: NUUID("85890483-16fd-4c17-ac7c-00004bcace85"),
    ParentCode: "",
    Code: "E",
    Weight: 2
},
{

    MatrixID: NUUID("00000000-0000-0000-0000-000000000000"),
    CompanyID: NUUID("85890483-16fd-4c17-ac7c-00004bcace85"),
    ParentCode: "E",
    Code: "E.1",
    Weight: 2
},
{

    MatrixID: NUUID("00000000-0000-0000-0000-000000000000"),
    CompanyID: NUUID("85890483-16fd-4c17-ac7c-00004bcace85"),
    ParentCode: "",
    Code: "E",
    Weight: 2
}

]);

 







db.company.insert([
{
    company: "one",
    matrix: "default",
    themes:
    [
        { P: "", N:"P", WR: ".25", W: ".25" },
        { P: "", N:"S", WR: ".25", W: ".25" },
        { P: "", N:"E", WR: ".25", W: ".25" },
        { P: "", N:"G", WR: ".25", W: ".25" }
    ],
    topics:
    [
        { P: "E", WR: ".25", W: ".25",  N:"P"},
        { P: "E", WR: ".25", W: ".25",  N:"S"},
        { P: "S", WR: ".25", W: ".25",  N:"E"},
        { P: "S", WR: ".25", W: ".25",  N:"G"},
    ],
    indicators:
    [
         { P: "E_1", WR: ".25", W: ".25", N:"E_1_1"},
         { P: "E_2", WR: ".25", W: ".25", N:"E_2_1"},
         { P: "S_1", WR: ".25", W: ".25", N:"S_1_1"},
         { P: "S_1", WR: ".25", W: ".25", N:"S_1_2"},
    ]
}
,

{
    company: "2",
    matrix: "default",
    themes:
    {
        E: { P: "", WR: ".25", W: ".25" },
        S: { P: "", WR: ".25", W: ".25" },
        P: { P: "", WR: ".25", W: ".25" },
        G: { P: "", WR: ".25", W: ".25" }
    },
    topics:
    {
        "E_1": { P: "E", WR: ".25", W: ".25" },
        "E_2": { P: "E", WR: ".25", W: ".25" },
        "S_1": { P: "S", WR: ".25", W: ".25" },
        "S_2": { P: "S", WR: ".25", W: ".25" },
    },
    indicators:
    {

        "E_1_1": { P: "E_1", WR: ".25", W: ".25" },
        "E_2_1": { P: "E_2", WR: ".25", W: ".25" },
        "S_1_1": { P: "S_1", WR: ".25", W: ".25" },
        "S_1_2": { P: "S_1", WR: ".25", W: ".25" },

    }
}
,

{
    company: "3",
    matrix: "default",
    themes:
    {
        E: { P: "", WR: ".25", W: ".25" },
        S: { P: "", WR: ".25", W: ".25" },
        P: { P: "", WR: ".25", W: ".25" },
        G: { P: "", WR: ".25", W: ".25" }
    },
    topics:
    {
        "E_1": { P: "E", WR: ".25", W: ".25" },
        "E_2": { P: "E", WR: ".25", W: ".25" },
        "S_1": { P: "S", WR: ".25", W: ".25" },
        "S_2": { P: "S", WR: ".25", W: ".25" },
    },
    indicators:
    {

        "E_1_1": { P: "E_1", WR: ".25", W: ".25" },
        "E_2_1": { P: "E_2", WR: ".25", W: ".25" },
        "S_1_1": { P: "S_1", WR: ".25", W: ".25" },
        "S_1_2": { P: "S_1", WR: ".25", W: ".25" },

    }
}
,

{
    company: "4",
    matrix: "default",
    themes:
    {
        E: { P: "", WR: ".25", W: ".25" },
        S: { P: "", WR: ".25", W: ".25" },
        P: { P: "", WR: ".25", W: ".25" },
        G: { P: "", WR: ".25", W: ".25" }
    },
    topics:
    {
        "E_1": { P: "E", WR: ".25", W: ".25" },
        "E_2": { P: "E", WR: ".25", W: ".25" },
        "S_1": { P: "S", WR: ".25", W: ".25" },
        "S_2": { P: "S", WR: ".25", W: ".25" },
    },
    indicators:
    {

        "E_1_1": { P: "E_1", WR: ".25", W: ".25" },
        "E_2_1": { P: "E_2", WR: ".25", W: ".25" },
        "S_1_1": { P: "S_1", WR: ".25", W: ".25" },
        "S_1_2": { P: "S_1", WR: ".25", W: ".25" },

    }
}

,

{
    company: "5",
    matrix: "default",
    themes:
    {
        E: { P: "", WR: ".25", W: ".25" },
        S: { P: "", WR: ".25", W: ".25" },
        P: { P: "", WR: ".25", W: ".25" },
        G: { P: "", WR: ".25", W: ".25" }
    },
    topics:
    {
        "E_1": { P: "E", WR: ".25", W: ".25" },
        "E_2": { P: "E", WR: ".25", W: ".25" },
        "S_1": { P: "S", WR: ".25", W: ".25" },
        "S_2": { P: "S", WR: ".25", W: ".25" },
    },
    indicators:
    {

        "E_1_1": { P: "E_1", WR: ".25", W: ".25" },
        "E_2_1": { P: "E_2", WR: ".25", W: ".25" },
        "S_1_1": { P: "S_1", WR: ".25", W: ".25" },
        "S_1_2": { P: "S_1", WR: ".25", W: ".25" },

    }
}


])






/*


db.doc.insert( 

   [{ a:100, b:1, c:1 },
   { a:1, b:1, c:1 },
   { a:1,  c:1 },
   { a:1,  c:1 },
   { a:1, b:1 },
   { a:1, b:1},
   { a:1, b:1, c:1 },
   {  b:1, c:1 },
   {  b:1, c:1 }]
)
   
db.doc.find( )
db.doc.find( {a:1})
db.doc.aggregate( [  { $group: {_id: "sumA", sumA: { $sum:"$a"}, avgA: { $avg:"$a"} ,countB: {$sum: 1},  sumB: { $sum:"$b"}, sumC: { $sum:"$c"}  }}]);

*/



db.doc2.insert([


    {
        cid: "1",
        mid: "0",
        theme: [
            {
                code: "E",
                weight: .25,

                topics: [ 

                    {
                        code: "E.1",
                        weight: .25,
                        indicators: [{ code: "E.1.1", weight: .1,  score: 5 }, { code: "E.1.3", weight: .1,  score: 5 }, { code: "E.1.4", weight: .1,  score: 5 }]
                    },
                    {
                        code: "E.2",
                        weight: .25,
                        indicators: [{ code: "E.2.1", weight: .1,  score: 5 }, { code: "E.2.3", weight: .1,  score: 5 }, { code: "E.2.4", weight: .1,  score: 5 }]
                    },
                    {
                        code: "E.3",
                        weight: .25,
                        indicators: [{ code: "E.3.2", weight: .1,  score: 5 }, { code: "E.3.3", weight: .1,  score: 5 }, { code: "E.3.4", weight: .1,  score: 5 }]
                    },
                    {
                        code: "E.4",
                        weight: .25,
                        indicators: [{ code: "E.4.2", weight: .1,  score: 5 }, { code: "E.4.3", weight: .1,  score: 5 }, { code: "E.4.4", weight: .1,  score: 5 }]
                    },
                ] 
            }
            ,
            {
                code: "S",
                weight: .25,

                topics: [

                    {
                        code: "S.1",
                        weight: .25,
                        indicators: [{ code: "S.1.1", weight: .1,  score: 5 }, { code: "S.1.3", weight: .1,  score: 5 }, { code: "S.1.4", weight: .1,  score: 5 }]
                    },
                    {
                        code: "S.2",
                        weight: .25,
                        indicators: [{ code: "S.2.1", weight: .1,  score: 5 }, { code: "S.2.3", weight: .1,  score: 5 }, { code: "S.2.4", weight: .1,  score: 5 }]
                    },                    
                    {
                        code: "S.3",
                        weight: .25,
                        indicators: [{ code: "S.3.2", weight: .1,  score: 5 }, { code: "S.3.3", weight: .1,  score: 5 }, { code: "S.3.4", weight: .1,  score: 5 }]
                    },
                    {
                        code: "S.4",
                        weight: .25,
                        indicators: [{ code: "S.4.2", weight: .1,  score: 5 }, { code: "S.4.3", weight: .1,  score: 5 }, { code: "S.4.4", weight: .1,  score: 5 }]
                    },
                ]
            }
            ,
            {
                code: "G",
                weight: .25,

                topics: [

                    {
                        code: "G.1",
                        weight: .25,
                        indicators: [{ code: "G.1.2", weight: .1,  score: 5 }, { code: "G.1.3", weight: .1,  score: 5 }, { code: "G.1.4", weight: .1,  score: 5 }]
                    },
                    {
                        code: "G.2",
                        weight: .25,
                        indicators: [{ code: "G.2.2", weight: .1,  score: 5 }, { code: "G.2.3", weight: .1,  score: 5 }, { code: "G.2.4", weight: .1,  score: 5 }]
                    },
                    {
                        code: "G.3",
                        weight: .25,
                        indicators: [{ code: "G.3.2", weight: .1,  score: 5 }, { code: "G.3.3", weight: .1,  score: 5 }, { code: "G.3.4", weight: .1,  score: 5 }]
                    },
                    {
                        code: "G.4",
                        weight: .25,
                        indicators: [{ code: "G.4.2", weight: .1,  score: 5 }, { code: "G.4.3", weight: .1,  score: 5 }, { code: "G.4.4", weight: .1,  score: 5 }]
                    },
                ]
            }
        ]
    }
]);


db.doc2result.drop()
//db.doc2.find()

db.doc2.aggregate([
{
    $unwind: "$theme"
}
,
{
    $group:
    {
        _id: "$_id",
        sumath: { $sum: "$theme.weight" },
        countth: { $sum: 1 },
        topics: { $push: { $sum: "$theme.topics.weight" } }

    }
}
,
{
    $unwind: "$topics"
}
,
{
    $unwind: "$topics"
}
,
{
    $group:
    {
        _id: "$_id",
        themes:
        {
            sumath: "$sumath",
            countth: "$countth"
        },
        sumto: { $sum: "$topics.weight" },
        countto: { $sum: 1 },
        //topics: { $addToSet: "$theme.topics"}

    }
}




]);

db.doc2result.find()



db.doc2.aggregate([
{ $unwind: "$theme" },
{
    $group:
      {
          _id:
          {
              _id: "$_id._id",
              cid: "$_id.cid",
              mid: "$_id.mid",
              companyScore: "$totalScore"
          }
            , E: { $sum: { $cond: [{ $eq: ["$theme.code", "E"] }, "$theme.score", ""] } }
            , S: { $sum: { $cond: [{ $eq: ["$theme.code", "S"] }, "$theme.score", ""] } }
            , G: { $sum: { $cond: [{ $eq: ["$theme.code", "G"] }, "$theme.score", ""] } }
            , P: { $sum: { $cond: [{ $eq: ["$theme.code", "P"] }, "$theme.score", ""] } }

      }
}

]);






